//
//  CZNewFeatureViewController.swift
//  iOSWeibo10
//
//  Created by 李伟鹏 on 16/5/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
private let ItemCount = 4
class CZNewFeatureViewController: UICollectionViewController {

    private let layout = UICollectionViewFlowLayout()
    
    init(){
        super.init(collectionViewLayout:layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
       // Register cell classes
        prepareCollectionView()

    }
    
    private func prepareCollectionView(){
        self.collectionView!.registerClass(CZNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
    }

}

extension CZNewFeatureViewController{
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ItemCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CZNewFeatureCell
        cell.index = indexPath.item
        return cell
    }
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let index = Int(scrollView.contentOffset.x / scrollView.frame.width)
        if index == ItemCount - 1 {
            let indexPath = NSIndexPath(forItem: index, inSection: 0)
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! CZNewFeatureCell
            cell.startAnimation()
            
        }
    }
}